/*
 * Created on Jun 13, 2004
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License");  you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * http//www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 *
 * The Original Code is MAPONTO.
 *
 * The Initial Developer of the Original Code is University of Toronto. Portions
 * created by University of Toronto are Copyright (C) 2004.  All Rights Reserved.
 *
 * MAPONTO was developed by Knowledge Management Lab
 * (http//www.cs.toronto.edu/km) at the University of Toronto Department of 
 * Computer Science. Current information about MAPONTO can be obtained at 
 * http//resi.ai.toronto.edu:8080
 *
 */

/*
 * Contributor(s): Yuan An yuana@cs.toronto.edu
 */
package main.util;

import java.io.*;
import java.util.*;
import java.net.*;

/**
 * com:bruceeckel:util:TextFile.java Static functions for reading and writing
 * text files as a single string, and treating a file as an ArrayList. {Clean:
 * test.txt test2.txt} Modification by Yuan An: ignore the comment and empty
 * lines.
 */
public class TextFile extends ArrayList {
	// Tools to read and write files as single strings:
	public static String read(String fileName) throws IOException,
			URISyntaxException {
		StringBuffer sb = new StringBuffer();
		BufferedReader in = new BufferedReader(new FileReader(new File(new URI(
				fileName))));
		String s;
		while ((s = in.readLine()) != null) {
			sb.append(s);
			sb.append("\n");
		}
		in.close();
		return sb.toString();
	}

	public static String read(File file) throws IOException {
		StringBuffer sb = new StringBuffer();
		BufferedReader in = new BufferedReader(new FileReader(file));
		String s;
		while ((s = in.readLine()) != null) {
			sb.append(s);
			sb.append("\n");
		}
		in.close();
		return sb.toString();
	}

	public static void write(String fileName, String text) throws IOException {
		PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(
				fileName)));
		out.print(text);
		out.close();
	}

	/*
	 * Append the text to the fileName.
	 */
	public static void writeAppend(String fileName, String text)
			throws IOException {
		PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(
				fileName, true)));
		out.print(text);
		out.close();
	}

	/**
	 * constructor.
	 * 
	 * @param fileName
	 * @throws IOException
	 */
	public TextFile(String fileName) throws IOException, URISyntaxException {
		super(Arrays.asList(read(fileName).split("\n")));
	}

	/**
	 * remove the comment lines and empty lines from the input string argument.
	 * 
	 * @param fileName
	 * @throws IOException
	 */
	public static String removeCommentsAndEmpties(String source) {
		String ans = "";
		// make an arraylist
		List array = Arrays.asList(source.split("\n"));
		for (int i = 0; i < array.size(); i++) {
			String ele = ((String) array.get(i)).trim();
			if (!ele.equals("") && !ele.startsWith("#")
					&& !ele.startsWith("DROP TABLE"))
				ans += ele;
		}
		return ans;
	}

	public void write(String fileName) throws IOException {
		PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(
				fileName)));
		for (int i = 0; i < size(); i++)
			out.println(get(i));
		out.close();
	}

	/**
	 * turn to a single string without newline symbols.
	 * 
	 * @param args
	 * @throws Exception
	 */
	public String toString() {
		String ans = "";
		for (int i = 0; i < size(); i++) {
			ans += (String) get(i);
		}
		return ans;
	}

	/**
	 * list all files in a directory
	 * 
	 * @param dir
	 * @return
	 */
	public static String listDirFiles(File file, ArrayList<Integer> count,
			PrintWriter output) {
		String ans = "";

		if (!file.exists()) {
			System.out.println(file + " does not exist.");
		}
		if (file.isDirectory()) {
			for (File f : file.listFiles()) {
				listDirFiles(f, count, output);
			}
		} else {
			String filename = file.getName();

			if (filename.endsWith("xml")) {
				int countValue = count.get(0).intValue() + 1;
				count.add(0, new Integer(countValue));
				output.println(filename);
				System.out.println(filename);
			} else {
				System.out.println("Skipped " + filename);
			}
		}

		return ans;
	}

	// Simple test:
	public static void main(String[] args) throws Exception {
		String dir = "C:/EMR-data";
		File file = new File(dir);
		ArrayList<Integer> count = new ArrayList<Integer>();
		count.add(new Integer(0));
		
		try {
			PrintWriter output = new PrintWriter(new File(
					"EMR-tables-extraction"));
			TextFile.listDirFiles(file, count, output);

			output.flush();

		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("Total files: " + count.get(0));
	}
}